package bishi.kaizi;

import java.util.*;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2022/03/14 16:14
 */
public class _10 {
    public static void main(String[] args) {
        int[][] res = solution("444 2000 445 544");
        for (int[] l :res){
            System.out.println(Arrays.toString(l));
        }
    }
    public static int[][] solution(String string) {
        List<int[]> list = new ArrayList<>();
        int nowCha = Integer.MAX_VALUE;
        int nowAll = Integer.MAX_VALUE;
        string = string.trim();
        if (string.length() == 0||string.replace(" ","").length()==0) return new int[0][0];
        String[] strs = string.split(" ");
        for (int i = 0; i < strs.length; i++) {
            int low = 10;
            int max = 0;
            int all = 0;
            for (char c : strs[i].toCharArray()) {
                int lastLow = low;
                low = Math.min(low, c - '0');
                max = Math.max(Math.max(max, lastLow), c - '0');
                all += c - '0';
            }
            if (max - low < nowCha) {
                nowCha = max - low;
                nowAll = all;
                list = new ArrayList<>();
                list.add(new int[]{nowAll, i, Integer.valueOf(strs[i])});
            } else if (max - low == nowCha) {
                if (nowAll == all) {
                    list.add(new int[]{nowAll, i, Integer.valueOf(strs[i])});
                } else if (nowAll < all) {
                    nowAll = all;
                    list = new ArrayList<>();
                    list.add(new int[]{nowAll, i, Integer.valueOf(strs[i])});
                }
            }
        }
        int[][] res = new int[list.size()][3];
        list.sort(Comparator.comparingInt(i -> i[1]));
        for (int i=0;i<list.size();i++){
            res[i] = list.get(i);
        }
        return res;
    }
}
